/**
 *
 */

package io.renren.controller;


import io.renren.annotation.Login;
import io.renren.common.exception.ErrorCode;
import io.renren.common.exception.RenException;
import io.renren.common.utils.HttpContextUtils;
import io.renren.common.utils.Result;
import io.renren.modules.health.dto.LoginDTO;
import io.renren.modules.health.service.HmCustomerService;
import io.renren.modules.health.service.TokenService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.stream.IntStream;

/**
 * 登录接口
 */
@RestController
@RequestMapping("/api")
@Api(tags = "登录接口")
public class ApiLoginController {
    @Autowired
    private TokenService tokenService;
    @Autowired
    private HmCustomerService hmCustomerService;

    @Autowired
    public static String SMS_DATA = "loginSmsData";

    private ConcurrentMap<String,LoginDTO> smsCodes = new ConcurrentHashMap<>();

    @PostMapping("login")
    @ApiOperation("登录")
    public Result<Map<String, Object>> login(@RequestBody @Validated LoginDTO dto) {
//        HttpSession session = HttpContextUtils.getHttpServletRequest().getSession();
//        LoginDTO smsData = (LoginDTO) session.getAttribute(SMS_DATA);
        LoginDTO smsData = smsCodes.get(SMS_DATA);
        if (smsData == null) throw new RenException(ErrorCode.SMS_CODE_NULL,"请先获取验证码");
        if (!dto.getPhonenum().equals(smsData.getPhonenum())) throw new RenException(ErrorCode.SMS_CODE_NULL,"请先获取验证码");
        if (!dto.getSmsCode().equals(smsData.getSmsCode())) throw new RenException(ErrorCode.SMS_CODE_ERROR,"验证码错误");

        //用户登录
        Map<String, Object> map = hmCustomerService.login(dto);
        return new Result<Map<String, Object>>().ok(map);
    }

    @Login
    @PostMapping("logout")
    @ApiOperation("退出")
    public Result<Object> logout(@ApiIgnore @RequestAttribute("userId") Long userId) {
        tokenService.expireToken(userId);
        return new Result<>();
    }

    @GetMapping("getSmsCode/{phonenum}")
    @ApiOperation("获取验证码")
    public Result<LoginDTO> getSmsCode(@PathVariable(name = "phonenum") String phonenum) {
        HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
        Random random = new Random();
        LoginDTO loginDTO = new LoginDTO();
        loginDTO.setPhonenum(phonenum);
        String smsCode = IntStream.range(0, 6).boxed().map(i -> String.valueOf(random.nextInt(10))).reduce((i, j) -> i + j).orElse("");
        loginDTO.setSmsCode(smsCode);
        request.getSession().setAttribute(SMS_DATA, loginDTO);
        smsCodes.put(SMS_DATA,loginDTO);
        return new Result<LoginDTO>().ok(loginDTO);
    }

}